package common.util.date;


/**
 *
 */
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;


/**
 * 日期处理
 */
public class DateUtil {
	
	public final static int ONE_DAY_SECOND = 86400;
	public final static int ONE_HOUR_SECOND = 3600;
	
	
	Calendar calendar; 	//日历
	Date date; 			//日期

	int year = 0; 		//年
	int month = 0; 		//月
	int day = 0; 		//天
	int week = 0; 		//周
	int hour = 0; 		//时
	int minute = 0; 	//分
	int second = 0; 	//秒
	long milDay = 24 * 60 * 60 * 1000;

	public DateUtil() {
		this.calendar = new GregorianCalendar();
		this.date = new Date();
		this.calendar.setTime(date);
	}

	/**
	 * 取一个特定的日期构造此方法
	 * 
	 * @param date
	 */
	public DateUtil(Date date) {
		this.calendar = new GregorianCalendar();
		this.date = date;
		this.calendar.setTime(date);
	}

	/**
	 * 判断当前日是否为给定日期的整数倍
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public boolean isDayMultiple(Date date, int amount) {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (this.calendar.before(c)) {
			long l = (c.getTime().getTime() - this.calendar.getTime().getTime())
					/ milDay;
			b = l % amount == 0 ? true : false;
		}
		return b;
	}

	/**
	 * 判断当前周是否为给定日期的整数倍
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public boolean isWeekMultiple(Date date, int amount) {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (this.calendar.before(c)) {
			long l = (c.getTime().getTime() - this.calendar.getTime().getTime())
					/ (milDay * 7);
			b = l % amount == 0 ? true : false;
		}
		return b;
	}

	/**
	 * 判断当前月是否为给定日期的整数倍（包含月份算法）
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public boolean isMonthMultiple(Date date, int amount) {
		boolean b = false;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (this.calendar.before(c)) {
			DateUtil util = new DateUtil(date);
			if (util.getDay() == getDay()) {
				int m1 = util.getMonth(), m2 = getMonth();
				int y = util.getYear() - getYear();
				if (y == 0) {
					b = (m1 - m2) % amount == 0 ? true : false;
				} else {
					b = (m1 + y * 12 - m2) % amount == 0 ? true : false;
				}
			}
		}
		return b;
	}

	/**
	 * 判断当前年是否为给定日期的整数倍
	 * 
	 * @param date
	 * @param amount
	 * @return
	 */
	public boolean isYearMultiple(Date date, int amount) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (this.calendar.before(c)) {
			DateUtil util = new DateUtil(date);
			if (util.getDay() == getDay() && util.getMonth() == getMonth()) {
				int t = util.getYear() - getYear();
				return t % amount == 0 ? true : false;
			}
		}
		return false;
	}
	
	/**
	 * 时间计算－秒数
	 * 
	 * @param iOffset
	 * @return
	 */
	public int addSecond(int iOffset) {
		this.calendar.add(Calendar.SECOND, iOffset);
		return this.getSecond();
	}

	/**
	 * 时间计算－分钟
	 * 
	 * @param iOffset
	 * @return
	 */
	public int addMinute(int iOffset) {
		this.calendar.add(Calendar.MINUTE, iOffset);
		return this.getMinute();
	}

	/**
	 * 时间计算－小时
	 * 
	 * @param iOffset
	 * @return
	 */
	public int addHour(int iOffset) {
		this.calendar.add(Calendar.HOUR_OF_DAY, iOffset);
		return this.getHour();
	}

	/**
	 * 日期计算－天数
	 * 
	 * @param iOffset
	 * @return int 计算过后的天数
	 */
	public int addDay(int iOffset) {
		this.calendar.add(Calendar.DAY_OF_MONTH, iOffset);
		return this.getDay();
	}

	/**
	 * 日期计算－月份
	 * 
	 * @param iOffset
	 * @return int 计算过后的月份
	 */
	public int addMonth(int iOffset) {
		this.calendar.add(Calendar.MONTH, iOffset);
		return this.getMonth();
	}

	/**
	 * 日期计算－年份
	 * 
	 * @param iOffset
	 * @return int 计算过后的年份
	 */
	public int addYear(int iOffset) {
		this.calendar.add(Calendar.YEAR, iOffset);
		return this.getYear();
	}

	// get method for any fields
	public int getYear() {
		this.year = this.calendar.get(Calendar.YEAR);
		return this.year;
	}

	public int getMonth() {
		this.month = this.calendar.get(Calendar.MONTH) + 1;
		return this.month;
	}

	public int getDay() {
		this.day = this.calendar.get(Calendar.DATE);
		return this.day;
	}

	public int getWeekDay() {
		return this.calendar.get(Calendar.DAY_OF_WEEK);
	}

	public int getWeek() {
		this.week = this.calendar.get(Calendar.WEEK_OF_MONTH);
		return this.week;
	}

	public int getHour() {
		this.hour = this.calendar.get(Calendar.HOUR_OF_DAY);
		return this.hour;
	}

	public int getMinute() {
		this.minute = this.calendar.get(Calendar.MINUTE);
		return this.minute;
	}

	public int getSecond() {
		this.second = this.calendar.get(Calendar.SECOND);
		return this.second;
	}
	
	
	/************************************************
	 * 以下为静态方法
	 ************************************************/

	/**
	 * 取得指定全格式，如 2012-6-13
	 * 
	 * @param date
	 * @return
	 */
	public static String getLocalDate(Date date) {
		Calendar local;
		local = Calendar.getInstance();
		if (date == null)
			return getToday();
		else {
			local.setTime(date);
			String localDate = String.valueOf(local.get(1)) + "-"
					+ String.valueOf(local.get(2) + 1) + "-"
					+ String.valueOf(local.get(5));
			return localDate;
		}
	}
	
	/**
	 * 取得指定全格式，如 2012-6-13
	 * 
	 * @param millisecond 标准基准时间以来的指定毫秒数
	 * @return
	 */
	public static String getLocalDate(long millisecond) {
		return getLocalDate(new Date(millisecond));
	}
	
	/**
	 * 取得指定全格式，如 2012-6-13
	 * 
	 * @param second 时间戳
	 * @return
	 */
	public static String getLocalDate(int second) {
		long millisecond = ((long) second) * 1000;
		return getLocalDate(millisecond);
	}

	/**
	 * 取得指定时间全格式，如 2012-6-13 10:44:55
	 * 
	 * @param date
	 * @return
	 */
	public static String getLocalTime(Date date) {
		Calendar local;
		local = Calendar.getInstance();
		if (date == null) {
			return getNow();
		} else {
			local.setTime(date);
			String localTime = String.valueOf(local.get(11)) + ":"
					+ String.valueOf(local.get(12)) + ":"
					+ String.valueOf(local.get(13));
			localTime = getLocalDate(date) + " " + localTime;
			return localTime;
		}
	}
	
	/**
	 * 取得指定时间全格式，如 2012-6-13 10:44:55
	 * 
	 * @param second 时间戳
	 * @return
	 */
	public static String getLocalTime(int second) {
		long millisecond = ((long)second) * 1000;
		return getLocalTime(millisecond);
	}
	
	/**
	 * 取得指定时间全格式，如 2012-6-13 10:44:55
	 * 
	 * @param millisecond 标准基准时间以来的指定毫秒数
	 * @return
	 */
	public static String getLocalTime(long millisecond) {
		return getLocalTime(new Date(millisecond));
	}

	/**
	 * 取得今天时间格式，如: 2012-6-13
	 * 
	 * @return
	 */
	public static String getToday() {
		Calendar rightNow;
		rightNow = Calendar.getInstance();
		String today = String.valueOf(rightNow.get(1)) + "-"
				+ String.valueOf(rightNow.get(2) + 1) + "-"
				+ String.valueOf(rightNow.get(5));
		return today;
	}

	/**
	 * 取得当前时间全格式，如2012-06-13 11:37:04.296
	 * 
	 * @return
	 */
	public static String getNow() {
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		return ts.toString();
	}
	
	/**
	 * 取得当前时间毫秒数
	 * 
	 * @return long
	 */
	public static long getNowMilliTime() {
		return System.currentTimeMillis();
	}

	/**
	 * 取得当前时间秒数(时间戳)
	 * 
	 * @return
	 */
	public static int getNowTime() {
		return getNowTime(getNowMilliTime());
	}
	
	/**
	 * 取得当前时间秒数(时间戳)
	 * 
	 * @return
	 */
	public static int getNowTime(long milliTime) {
		return (int) (milliTime / 1000);
	}

	public static Date getCurrentDate() {
		Date d = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			d = format.parse(getToday());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return d;
	}

	/**
	 * 转换日期字符串为格式化日期(yyyy-MM-dd)
	 * 
	 * @param dateStr
	 * @return d
	 */
	public static Date getFormatDate(String dateStr) {	
		return getFormatDateTime("yyyy-MM-dd", dateStr);
	}
	
	/**
	 * 转换日期字符串为格式化日期(yyyy-MM-dd HH:ii:ss)
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date getFormatDateTime(String dateStr) {
		return getFormatDateTime("yyyy-MM-dd HH:mm:ss", dateStr);
	}
	
	/**
	 * 返回给定日期的时间戳
	 * 
	 * @param dateStr 日期格式(yyyy-MM-dd HH:ii:ss)
	 * @return
	 */
	public static int getDateTime(String dateStr) {
		Date date = getFormatDateTime(dateStr);
		return (int) (date.getTime()/1000L);
	}
	
	/**
	 * 转换日期字符串为格式化日期
	 * @param pattern
	 * @param dateStr
	 * @return
	 */
	public static Date getFormatDateTime(String pattern, String dateStr) {
		if ( dateStr.equals("") || dateStr == null ) {
			dateStr = getLocalTime(new Date());
		}
		
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		try {
			date = format.parse(dateStr);
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 日期格式化
	 * 
	 * @param format
	 *            format:<br>
	 *            1. month(English adv.): E<br>
	 *            2. day: dd<br>
	 *            3. week: MMM<br>
	 *            4. year: yyyy<br>
	 *            5. hour: HH<br>
	 *            6. minutes: mm<br>
	 *            7. second: ss<br>
	 *            8. locate: Z<br>
	 * @return String the formated string
	 */
	public static final String getFormattedDate(String format) {
		return (new SimpleDateFormat(format)).format(new java.util.Date());
	}
	
	public static long getNextResetTime(int hour, int min, int second){
//		Date curDate = new Date();
//		long curTime = curDate.getTime();
//		Calendar today = Calendar.getInstance(TimeZone.getTimeZone("GMT-8"));
//		today.set(Calendar.HOUR, hour);
//		today.set(Calendar.MINUTE, min);
//		today.set(Calendar.SECOND, second);
//		long resetTime = today.getTimeInMillis();
//		if(curTime >= resetTime){
//			today.add(Calendar.DAY_OF_MONTH, 1);
//			return today.getTimeInMillis();
//		}else{
//			return resetTime;
//		}
		return 1000*getNextResetTimeInt(hour, min, second);
	}
	
	public static int getNextResetTimeInt(int hour, int min, int second){
//		Date curDate = new Date();
//		curDate.setHours(hour);
//		curDate.setMinutes(min);
//		curDate.setSeconds(second);
		String today = DateUtil.getToday();
		today=today+" "+hour+":"+min+":"+second; 
		int setTime = DateUtil.getDateTime(today); 
		int now  =  DateUtil.getNowTime();
		if(now>=setTime){
			setTime = setTime +60*60*24;
		}
		return setTime;
//		int curTime = getNowTime();
//		Calendar today = Calendar.getInstance();
//		today.set(Calendar.HOUR, hour);
//		today.set(Calendar.MINUTE, min);
//		today.set(Calendar.SECOND, second);
//		today.setTime(curDate);
////		int resetTime = (int)(today.getTimeInMillis()/1000);
//		int resetTime = (int)(today.getTime().getTime()/1000);
//		if(curTime >= resetTime){
//			today.add(Calendar.DAY_OF_MONTH, 1);
//			return (int)(today.getTimeInMillis()/1000);
//		}else{
//			return resetTime;
//		}
	}

	public static void main(String[] args) {
//		String dateFrom = "2013-05-24 21:00:00";
//		String dateTo = "2013-04-29 23:59:59";
//		int ifrom = DateUtil.getDateTime(dateFrom);
//		int ito = DateUtil.getDateTime(dateTo);
//		System.out.println(ifrom);
		//System.out.println(ito);
		String date = "2013-08-10 23:59:59";
		System.out.println(DateUtil.getDateTime(date));
		date = "2013-08-10 00:00:00";
		System.out.println(DateUtil.getDateTime(date));
		System.out.println(getLocalTime(1376138284));
	}
}
